/*
 * @Description: 
 * @Version: 2.0
 * @Autor: 张磊
 * @Date: 2020-11-13 13:52:11
 * @LastEditors: 张磊
 * @LastEditTime: 2020-11-13 23:07:41
 * @FilePath: \lht1802A\项目代码\rank_tree\src\store\modules\classify.js
 */
import {
  getSortList,
  getSearchList,
  getClickSearchList,
  getChangeList
} from '@/api/index'
const state = {
  leftTab: [],
  searchData: [],
  sortList: [], //排行数据
  conList: [], //内容数据
  historyData: (JSON.parse(localStorage.getItem('historyData'))&&JSON.parse(localStorage.getItem('historyData')).slice(0, 12)) || [],
  changeList: [],
}
const mutations = {
  getSortList(state, val) {
    state.leftTab = val
    // console.log(val);
  },
  getSearchList(state, val) {
    state.searchData = val
  },
  getClickSearchList(state, val) {
    state.sortList = val.rankList,
      state.conList = val.articleList
    // console.log(val, 'aaaaa');
  },
  getChangeList(state, val) {
    state.changeList = val
    // console.log(val, 'aaaaa');
  },
  //添加历史记录
  addHistory(state, val) {
    state.historyData.unshift(val)
    localStorage.setItem('historyData', JSON.stringify(state.historyData))
  }
}
const actions = {
  async getSortList({
    commit
  }, val) {
    let res = await getSortList(val)
    commit('getSortList', res.dataList)
    console.log(res.dataList);
  },
  //搜索页数据
  async getSearchList({
    commit
  }, val) {
    let res = await getSearchList(val)
    commit('getSearchList', res.dataList);
    console.log(res.dataList);
  },
  //点击搜索数据
  async getClickSearchList({
    commit
  }, val) {
    let res = await getClickSearchList(val)
    console.log(res, 'ssssssss');
    // commit('getClickSearchList', res.recommendRankList.length ? res.recommendRankList : res.rankList);
    commit('getClickSearchList', res)
  },
  //改变输入框数据
  async getChangeList({
    commit
  }, val) {
    let res = await getChangeList(val)
    // console.log(res, 'ssssssss');
    commit('getChangeList', res.dataList);
  },
  //添加历史记录
  async addHistory({
    commit
  }, val) {
    commit('addHistory', val);
  },
}

const modules = {}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  modules
}
